import { AxiosResponse } from "axios";
import {
  ExportParam,
  IServiceApi,
  JxtechInputBean,
  JxtechSystemParam,
  ReturnPage,
  SystemParam
} from "@whjxtech/jxtech-component";
import { SYSTEM_CONFIG } from "@/config/system";
import { objectToUrlParams } from "@/utils/common";
import service from "@/api";

export class ServiceApi implements IServiceApi {
  //primaryKey: string;
  protected baseUrl: string; //例 /org/user => list url => /org/user/list
  /**
   * 需要传入baseUrl
   * @param baseUrl baseurl 例：/org/user
   */
  constructor(baseUrl: string) {
    if (!baseUrl) throw new Error("baseUrl not null.");
    this.baseUrl = baseUrl;
  }
  add(params?: any): Promise<AxiosResponse<any>> {
    let url = this.baseUrl + "/add";
    return service.get(url, { params });
  }
  delete(pk: string, params?: any): Promise<AxiosResponse<any>> {
    let url = this.baseUrl + "/delete?pk=" + pk;
    url += objectToUrlParams(params, false);
    return service.post(url);
  }
  exportData(exportParam?: ExportParam | null, params?: any): Promise<AxiosResponse<Blob>> {
    //默认导出 querylist
    let queryName = "QueryList";
    if (exportParam && exportParam.queryName) {
      queryName = exportParam.queryName[0].toLocaleUpperCase() + exportParam.queryName.substring(1, exportParam.queryName.length);
      delete exportParam.queryName;
    }
    let url = this.baseUrl + "/export" + queryName;
    url += objectToUrlParams({ ...params, ...exportParam }, true);
    return service.post(url, null, {
      responseType: "blob",
      timeout: SYSTEM_CONFIG.DOWNLOAD_FILE_TIMEOUT
    });
  }
  queryPage(systemParam?: JxtechSystemParam | null, queryParam?: any): Promise<AxiosResponse<ReturnPage>> {
    let queryName = "QueryList";
    if (systemParam && systemParam.queryName) {
      queryName = systemParam.queryName[0].toLocaleUpperCase() + systemParam.queryName.substring(1, systemParam.queryName.length);
      delete systemParam.queryName;
    }
    let url = this.baseUrl + "/page" + queryName;
    return service.get(url, { params: { ...systemParam, ...queryParam } });
  }
  query(queryParam?: SystemParam | null, params?: any): Promise<AxiosResponse<any>> {
    let url = this.baseUrl + "/query";
    return service.get(url, { params: { ...params, ...queryParam } });
  }
  queryById(pk: string, params?: any): Promise<AxiosResponse<any>> {
    let url = this.baseUrl + "/queryById?pk=" + pk;
    return service.get(url, { params });
  }
  queryList(systemParam?: JxtechSystemParam | null, queryParam?: any): Promise<AxiosResponse<any[]>> {
    let queryName = "queryList";
    if (systemParam && systemParam.queryName) {
      queryName = systemParam.queryName;
      delete systemParam.queryName;
    }
    let url = this.baseUrl + "/" + queryName;
    return service.get(url, { params: { ...systemParam, ...queryParam } });
  }
  saveInsert(bean: any, params?: any): Promise<AxiosResponse<any>> {
    let url = this.baseUrl + "/saveInsert";
    url += objectToUrlParams(params, true);
    return service.post(url, bean);
  }
  saveMulti(inputBean: JxtechInputBean, params?: any): Promise<AxiosResponse<void>> {
    let url = this.baseUrl + "/saveMulti";
    url += objectToUrlParams(params, true);
    if (params && params.inputBean) delete params.inputBean;
    return service.post(url, inputBean);
  }
  saveMultiDelete(pkList: string[], params?: any): Promise<AxiosResponse<number>> {
    let url = this.baseUrl + "/saveMultiDelete";
    url += objectToUrlParams(params, true);
    if (params && params.pkList) delete params.pkList;
    return service.post(url, pkList);
  }
  saveUpdate(bean: any, params?: any): Promise<AxiosResponse<any>> {
    let url = this.baseUrl + "/saveUpdate";
    url += objectToUrlParams(params, true);
    return service.post(url, bean);
  }
  saveMasterDetail(status: "insert" | "update", master: any, detail: JxtechInputBean): Promise<AxiosResponse<any>> {
    let url = this.baseUrl + "/saveMasterDetail";
    return service.post(url, { status, master, detail });
  }
}
